package com.qilu.careprogram.controller.user;

import com.qilu.careprogram.model.Result;
import com.qilu.careprogram.pojo.dto.AddDrugDTO;
import com.qilu.careprogram.pojo.dto.ChangeDrugUseDTO;
import com.qilu.careprogram.pojo.dto.UpdateDrugDTO;
import com.qilu.careprogram.pojo.entity.Drug;
import com.qilu.careprogram.pojo.entity.Carousel;
import com.qilu.careprogram.pojo.dto.*;
import com.qilu.careprogram.pojo.entity.HotArticle;
import com.qilu.careprogram.pojo.dto.NurseInfoDTO;
import com.qilu.careprogram.pojo.dto.ProstateSurgeryInfoDTO;
import com.qilu.careprogram.pojo.dto.UserQuestionDTO;
import com.qilu.careprogram.pojo.entity.PatientInfo;
import com.qilu.careprogram.pojo.vo.PatientInfoVO;
import com.qilu.careprogram.service.PatientService;
import com.qilu.careprogram.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.qilu.careprogram.pojo.dto.PatientLoginDTO;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/patient")
@Slf4j
public class PatientController {

    @Resource
    private PatientService patientService;

    @Resource
    private QuestionService questionService;

    /**
     * 获取用户收藏的文章ID列表
     */
    @PostMapping("/getUserLikeHotArticlesId")
    public Result getUserLikeHotArticlesId(@RequestBody UserLikeDTO dto) {
        try {
            List<Long> articleIds = patientService.getUserLikeHotArticlesId(dto.getHospitalNumber());
            return Result.success(articleIds);
        } catch (Exception e) {
            log.error("获取收藏文章ID失败", e);
            return Result.error("获取收藏文章ID失败: " + e.getMessage());
        }
    }

    /**
     * 取消收藏文章
     */
    @PostMapping("/deleteLike")
    public Result deleteLike(@RequestBody UserLikeDTO dto) {
        try {
            patientService.deleteLike(dto.getHospitalNumber(), dto.getHotArticleId());
            return Result.success("取消收藏成功");
        } catch (Exception e) {
            log.error("取消收藏失败", e);
            return Result.error("取消收藏失败: " + e.getMessage());
        }
    }

    /**
     * 收藏文章
     */
    @PostMapping("/addLike")
    public Result addLike(@RequestBody UserLikeDTO dto) {
        try {
            patientService.addLike(dto.getHospitalNumber(), dto.getHotArticleId());
            return Result.success("收藏成功");
        } catch (Exception e) {
            log.error("收藏失败", e);
            return Result.error("收藏失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户收藏的文章详情列表
     */
    @PostMapping("/getUserLike")
    public Result getUserLike(@RequestBody UserLikeDTO dto) {
        try {
            List<HotArticle> articles = patientService.getUserLike(dto.getHospitalNumber());
            return Result.success(articles);
        } catch (Exception e) {
            log.error("获取收藏文章详情失败", e);
            return Result.error("获取收藏文章详情失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有轮播图
     */
    @GetMapping("/getAllCarousel")
    public Result getAllCarousel() {
        try {
            List<Carousel> carousels = patientService.getAllCarousel();
            return Result.success(carousels);
        } catch (Exception e) {
            log.error("获取轮播图列表失败", e);
            return Result.error("获取轮播图列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有热门文章
     */
    @GetMapping("/getAllHotArticles")
    public Result getAllHotArticles() {
        try {
            List<HotArticle> articles = patientService.getAllHotArticles();
            return Result.success(articles);
        } catch (Exception e) {
            log.error("获取热门文章列表失败", e);
            return Result.error("获取热门文章列表失败: " + e.getMessage());
        }
    }

    /**
     * 添加轮播图
     */
    @PostMapping("/addCarousel")
    public Result addCarousel(@RequestBody AddCarouselDTO addCarouselDTO) {
        try {
            patientService.addCarousel(addCarouselDTO);
            return Result.success("轮播图添加成功");
        } catch (Exception e) {
            log.error("添加轮播图失败", e);
            return Result.error("添加轮播图失败: " + e.getMessage());
        }
    }

    /**
     * 添加热门文章
     */
    @PostMapping("/addHotArticles")
    public Result addHotArticles(@RequestBody AddHotArticleDTO addHotArticleDTO) {
        try {
            patientService.addHotArticle(addHotArticleDTO);
            return Result.success("热门文章添加成功");
        } catch (Exception e) {
            log.error("添加热门文章失败", e);
            return Result.error("添加热门文章失败: " + e.getMessage());
        }
    }

    /**
     * 修改轮播图启用状态
     */
    @PostMapping("/changeCarouselIsused")
    public Result changeCarouselIsused(@RequestBody ChangeCarouselIsusedDTO dto) {
        try {
            patientService.changeCarouselIsused(dto);
            return Result.success("轮播图状态修改成功");
        } catch (Exception e) {
            log.error("修改轮播图状态失败", e);
            return Result.error("修改轮播图状态失败: " + e.getMessage());
        }
    }

    /**
     * 修改热门文章启用状态
     */
    @PostMapping("/changeHotArticlesIsused")
    public Result changeHotArticlesIsused(@RequestBody ChangeHotArticleIsusedDTO dto) {
        try {
            patientService.changeHotArticleIsused(dto);
            return Result.success("热门文章状态修改成功");
        } catch (Exception e) {
            log.error("修改热门文章状态失败", e);
            return Result.error("修改热门文章状态失败: " + e.getMessage());
        }
    }

    /**
     * 修改轮播图信息
     */
    @PostMapping("/changeCarousel")
    public Result changeCarousel(@RequestBody UpdateCarouselDTO dto) {
        try {
            patientService.updateCarousel(dto);
            return Result.success("轮播图修改成功");
        } catch (Exception e) {
            log.error("修改轮播图失败", e);
            return Result.error("修改轮播图失败: " + e.getMessage());
        }
    }

    /**
     * 修改热门文章信息
     */
    @PostMapping("/changeHotArticles")
    public Result changeHotArticles(@RequestBody UpdateHotArticleDTO dto) {
        try {
            patientService.updateHotArticle(dto);
            return Result.success("热门文章修改成功");
        } catch (Exception e) {
            log.error("修改热门文章失败", e);
            return Result.error("修改热门文章失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有药品信息
     */
    @GetMapping("/getAllDrug")
    public Result getAllDrug() {
        try {
            List<Drug> drugs = patientService.getAllDrug();
            return Result.success(drugs);
        } catch (Exception e) {
            log.error("获取药品列表失败", e);
            return Result.error("获取药品列表失败: " + e.getMessage());
        }
    }

    /**
     * 修改药品启用状态
     */
    @PostMapping("/changeDrugUse")
    public Result changeDrugUse(@RequestBody ChangeDrugUseDTO dto) {
        try {
            boolean success = patientService.changeDrugUse(dto.getId(), dto.getIsused());
            if (success) {
                return Result.success("操作成功");
            } else {
                return Result.error("药品不存在或状态未变更");
            }
        } catch (Exception e) {
            log.error("修改药品状态失败", e);
            return Result.error("修改药品状态失败: " + e.getMessage());
        }
    }

    /**
     * 添加药品
     */
    @PostMapping("/addDrug")
    public Result addDrug(@RequestBody AddDrugDTO dto) {
        try {
            boolean success = patientService.addDrug(dto);
            if (success) {
                return Result.success("添加药品成功");
            } else {
                return Result.error("添加药品失败");
            }
        } catch (Exception e) {
            log.error("添加药品失败", e);
            return Result.error("添加药品失败: " + e.getMessage());
        }
    }

    /**
     * 修改药品信息
     */
    @PostMapping("/changeDrug")
    public Result changeDrug(@RequestBody UpdateDrugDTO dto) {
        try {
            boolean success = patientService.changeDrug(dto);
            if (success) {
                return Result.success("修改药品成功");
            } else {
                return Result.error("药品不存在或信息未变更");
            }
        } catch (Exception e) {
            log.error("修改药品信息失败", e);
            return Result.error("修改药品信息失败: " + e.getMessage());
        }
    }

    /**
            * 查询复查信息
     * @param hospitalNumber 住院号
     * @param reviewItem 复查项目
     * @param startTime 预约开始时间
     * @param endTime 预约结束时间
     * @return 查询结果
     */
    @GetMapping("/fuchaxinxi")
    public Result getReviewInfo(
            @RequestParam(required = false) String hospitalNumber,
            @RequestParam(required = false) String reviewItem,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        try {
            return Result.success(patientService.getReviewInfo(hospitalNumber, reviewItem, startTime, endTime));
        } catch (Exception e) {
            log.error("查询复查信息失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询患者信息
     * @param gender 性别
     * @param startAge 起始年龄
     * @param endAge 结束年龄
     * @param disease 疾病类型
     * @param isRegister 注册情况
     * @return 查询结果
     */
    @GetMapping("/selectPatientInfo")
    public Result selectPatientInfo(@RequestParam String gender, @RequestParam int startAge, @RequestParam int endAge, @RequestParam String disease, @RequestParam String isRegister) {
        try {
            return Result.success(patientService.selectPatientInfo(gender, startAge, endAge, disease, isRegister));
        } catch (Exception e) {
            log.error("查询患者信息失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    //    用户页面中修改性别年龄和手机号的接口
    @PostMapping("/changePatientInfo")
    public Result<Map<String, Object>> changePatientInfo(@RequestBody PatientInfo patientInfo) {
        try {
            return patientService.changePatientInfo(patientInfo);
        } catch (Exception e) {
            log.info(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/checkInfoWrite")
    public Result<String> checkInfoWrite(@RequestParam String hospitalNumber) {
        try {
            return patientService.checkInfoWrite(hospitalNumber);
        } catch (Exception e) {
            log.error("信息检查失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 通过住院号拿到病人历史信息
     * @param hospitalNumber
     * @return
     */
    @GetMapping("/getPatientInfo")
    public Result<PatientInfoVO> getPatientInfo(@RequestParam String hospitalNumber) {
        PatientInfoVO patientInfoVO = patientService.getPatientInfoByHospitalNumber(hospitalNumber);

        return Result.success(patientInfoVO);
    }

    /**
     * 病人个人信息和造口护理信息填报
     * @param nurseInfoDTO
     * @return
     */
    @PostMapping("/insertNurseInfo")
    public Result insertNurseInfo(@RequestBody NurseInfoDTO nurseInfoDTO) throws Exception {

        patientService.insertNurseInfo(nurseInfoDTO);

        return Result.success();
    }

    @PostMapping("/insertProstateSurgeryInfo")
    public Result insertProstateSurgeryInfo(@RequestBody ProstateSurgeryInfoDTO prostateSurgeryInfoDTO) throws Exception {
        log.info("controller: insertProstateSurgeryInfo is ", prostateSurgeryInfoDTO.toString());
        patientService.insertProstateSurgeryInfo(prostateSurgeryInfoDTO);

        return Result.success();
    }

    /**
     * 问题填报
     * @param userQuestionDTO
     * @return
     */
    @PostMapping("/insertQuestion")
    public Result insertQuestion(@RequestBody UserQuestionDTO userQuestionDTO) {

        questionService.insert(userQuestionDTO);

        return Result.success();
    }

    /**
     * 新增住院号
     * @param hospitalNumber 住院号
     * @param authority 科室
     * @param idCard 身份证号
     * @param limRevisitTime 回访周期
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result<String> addPatient(
            @RequestParam String hospitalNumber,
            @RequestParam String authority,
            @RequestParam String idCard,
            @RequestParam String limRevisitTime) {
        try {
            patientService.addPatient(hospitalNumber, authority, idCard, limRevisitTime);
            return Result.success("成功添加住院号" + hospitalNumber);
        } catch (Exception e) {
            log.error("添加住院号失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 删除住院号
     * @param hospitalNumber 住院号
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Result<String> deletePatient(@RequestParam String hospitalNumber) {
        try {
            patientService.deletePatient(hospitalNumber);
            return Result.success("成功删除住院号: " + hospitalNumber);
        } catch (Exception e) {
            log.error("删除住院号失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 患者注册接口
     * @param patientLoginDTO 包含住院号、用户名和密码的 DTO
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody PatientLoginDTO patientLoginDTO) {
        try {
            return patientService.register(patientLoginDTO);
        } catch (Exception e) {
            log.error("患者注册失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 发送验证码接口
     * @param phoneNumber 用户手机号
     * @return 发送结果
     */
    @PostMapping("/sendVerificationCode")
    public Result<String> sendVerificationCode(@RequestParam String phoneNumber) {
        try {
            return patientService.sendVerificationCode(phoneNumber);
        } catch (Exception e) {
            log.error("验证码发送失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 重置密码接口
     * @param patientLoginDTO 包含住院号、新用户名、新密码、手机号的 DTO
     * @return 重置结果
     */
    @PostMapping("/resetKey")
    public Result<String> resetKey(@RequestBody PatientLoginDTO patientLoginDTO) {
        try {
            return patientService.resetKey(patientLoginDTO);
        } catch (Exception e) {
            log.error("重置密码失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 患者登录接口
     * @param patientLoginDTO 包含用户名和密码的 DTO
     * @return 登录结果，成功则返回 token
     */
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody PatientLoginDTO patientLoginDTO) {
        try {
            return patientService.login(patientLoginDTO);
        } catch (Exception e) {
            log.error("患者登录失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}
